package com.wx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.*;
import com.wx.constants.Constants;
import com.wx.entity.SysNurse;
import com.wx.holder.SecurityUserHolder;
import com.wx.service.SysNurseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * <p>Title:      SysNurseController. </p>
 * <p>Description  </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-03 11:36:07
 */
@RestController
@RequestMapping(value = "/sysNurse", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "护士管理", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysNurseController {

    @Resource
    private SysNurseService sysNurseService;

    @Resource
    private SecurityUserHolder securityUserHolder;


    @PostMapping("/listPage")
    @ApiOperation(httpMethod = "POST", value = "分页列表查询")
    public Wrapper<PageInfo<SysNurse>> listPage(@ApiParam(name = "SysNurse", value = "SysNurse") @RequestBody PageParam<SysNurse> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<SysNurse> list = sysNurseService.listPage(pageParam.getParam());
        PageInfo<SysNurse> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }


    @PostMapping(value = "/export")
    @ApiOperation(httpMethod = "POST", value = "导出护士")
    public void export(HttpServletResponse response, @ApiParam(name = "SysNurse", value = "SysNurse") @RequestBody SysNurse sysNurse) throws IOException {
        List<SysNurse> list = sysNurseService.listPage(sysNurse);
        //添加响应头信息
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("护士列表.xlsx", "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //获取数据
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 15);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(response.getOutputStream(), SysNurse.class).registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("护士列表").doWrite(list);
    }


    /**
     * <p>Title:      save. </p>
     * <p>Description 保存用户信息</p>
     *
     * @param user the user
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/22 17:24
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增")
    public Wrapper<Integer> saveUserInfo(@ApiParam(name = "nurse", value = "信息") @RequestBody @Validated(CreateValidate.class) SysNurse user) {
        SecurityUser securityUser = securityUserHolder.getUser();
        user.setCreateBy(securityUser.getUserId());
        user.setCreator(securityUser.getRealName());
        user.setUpdateBy(securityUser.getUserId());
        user.setUpdater(securityUser.getRealName());
        int result = sysNurseService.saveNurse(user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    /**
     * <p>Title:      modifyUser. </p>
     * <p>Description 修改用户信息</p>
     *
     * @param user the user
     * @return wrapper wrapper
     * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
     * @since 2018 /2/20 15:38
     */
    @PostMapping(value = "/update")
    @ApiOperation(httpMethod = "POST", value = "修改")
    public Wrapper<Integer> modifyUser(@ApiParam(name = "nurse", value = "用户信息") @RequestBody @Validated(UpdateValidate.class) SysNurse user) {
        SecurityUser securityUser = securityUserHolder.getUser();
        user.setUpdateBy(securityUser.getUserId());
        user.setUpdater(securityUser.getRealName());
        int result = sysNurseService.update(user);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }


    @PostMapping("/list")
    @ApiOperation(httpMethod = "POST", value = "下拉列表查询所有护士信息(审核通过的)")
    public Wrapper<List<SysNurse>> list() {
        SysNurse query = new SysNurse();
        query.setDelFlag(Constants.DELETE_NO);
        query.setAuditStatus(Constants.YES);
        List<SysNurse> list = sysNurseService.select(query);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }



    @PostMapping(value = "/update/{id}/{status}")
    @ApiOperation(httpMethod = "POST", value = "审核状态")
    public Wrapper<Integer> updateStatus(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        SecurityUser securityUser = securityUserHolder.getUser();
        int result = sysNurseService.updateStatus(id,status,securityUser.getUserId(),securityUser.getRealName());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

}